//
//  WSGPlayerKillingFieldController.m
//  oneDollar
//
//  Created by Laki on 16/6/15.
//  Copyright © 2016年 com.Laki.corder. All rights reserved.
//

#import "WSGPlayerKillingFieldController.h"
#import "WSGPlayerKillingFieldCell.h"
#import "WSGBuyLabel.h"

#import "WSGHttpTool.h"
#import "WSGPlayerItem.h"

#import <MJRefresh/MJRefresh.h>
#import <MJExtension/MJExtension.h>

/** 请求路径*/
#define PlayerURL @"http://api.g.caipiao.163.com/yyg/pk/pk_list.html?product=yiyuangou&mobileType=iphone&ver=1.6.1&channel=25hyg&apiVer=1.1&apiLevel=23&deviceId=A5209034-6978-4233-9E93-F88A282F1F98&pageNo=1&pageSize=20"

@interface WSGPlayerKillingFieldController ()
/** 数组模型*/
@property (nonatomic,strong) NSArray *playerM;

@end

@implementation WSGPlayerKillingFieldController

/** 重用标识符*/
static NSString * const ID = @"WSGPlayerKillingFieldCell";
/** 内容的头部高度*/
static NSInteger const contentInsetTop = 30;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置下拉刷新
    [self setRefresh];
    
    // 设置背景颜色
    self.view.backgroundColor = [UIColor colorWithRed:241.0/255.0 green:241.0/255.0 blue:241.0/255.0 alpha:1.0];
    
    // 隐藏分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    // 设置内边距
    self.tableView.contentInset = UIEdgeInsetsMake(contentInsetTop, 0, 0, 0);

    // 注册Cell
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([WSGPlayerKillingFieldCell class]) bundle:nil] forCellReuseIdentifier:ID];
}



#pragma mark --------------------
#pragma mark 刷新数据
/*
 * 设置下拉刷新
 */
- (void)setRefresh{
    // 创建默认下拉刷新
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    
    // 设置下拉刷新
    self.tableView.mj_header = header;
    
    // 忽略多少scrollView的contentInset的top
    self.tableView.mj_header.ignoredScrollViewContentInsetTop = contentInsetTop;
    
    // 开始刷新
    [self.tableView.mj_header beginRefreshing];
}

/*
 * 加载数据
 */
- (void)loadData{
    // 发送GET请求
    [WSGHttpTool get:PlayerURL parameters:nil success:^(id responseObj) {
        // 字典转模型
        self.playerM = [WSGPlayerItem mj_objectArrayWithKeyValuesArray:responseObj[@"productInfosList"]];
        
        // 创建一个Label
        WSGBuyLabel *label = [[WSGBuyLabel alloc] init];
        // 设置文字
        label.text = responseObj[@"headerSlogan"];
        // 添加
        [self.view addSubview:label];
        
        // 刷新数据
        [self.tableView reloadData];
        
        // 停止下拉刷新
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark --------------------
#pragma mark <UITableViewDataSource>
/*
 * 显示多少行Cell
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.playerM.count;
}

/*
 * 每行Cell展示什么数据
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    // 从缓存池中去Cell
    WSGPlayerKillingFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    // 传递模型数据
    cell.item = self.playerM[indexPath.row];
    
    // 设置Cell选中样式
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

/*
 * 每行Cell的高度
 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 145;
}
@end
